/**
 * @class   VGettyAMImpl
 * @brief   
 ****************************************************************************
 * @version $Id: VGettyAMImpl.h 2488 2010-05-21 01:48:27Z alex $
 * @author  Alessandro Polo
 * @date    Released on 2009-06-04
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_DEVICES_VGettyAM_H__
 #define __WOSH_DEVICES_VGettyAM_H__

 #include <woshDefs.h>
 #include <core/Containers.h>
 #include <core/Mutex.h>
 #include <core/BundleGenericWorker.h>

 #include <framework/network/SocketUdp.h>

 #include <framework/communication/AudioMessage.h>
 #include <framework/communication/VoiceCall.h>


 #include "IVGettyAMListener.h"



class VGettyAMImpl :	public wosh::BundleGenericWorker,
						public virtual wosh::network::ISocketUdpListener
 {

	typedef wosh::ListT<wosh::communication::VoiceCall*> tVoiceCallList;
	typedef wosh::ListT<wosh::communication::AudioMessage*> tAudioMessageList;


	public:
		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 */
		VGettyAMImpl( wosh::BundleGeneric& bundle );
		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~VGettyAMImpl();


/** @name Setters
 * @{
 ******************************************************************************/
	public:
		WRESULT setDialScript( const std::string& script_path );
		WRESULT setCtrlScript( const std::string& script_path );

		WRESULT setBindAddressSlave( const std::string& bind_ip );
		WRESULT setBindAddressDevice( const std::string& address_port );

		WRESULT setFolderMessageStored( const std::string& folder_path );
		WRESULT setFolderMessageIncoming( const std::string& folder_path );

		WRESULT setMessageGreeting( const std::string& message_file );

		/**
		 * @brief   Set the IVGettyAMListener interface for callbacks.
		 * @param listener [in] pointer to object which implements callback-functions
		 * @note    Call this method only once, after creation, it's not reetrant!
		 */
		void setVGettyAMListener( IVGettyAMListener* listener );

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		const std::string& getProcessId() const				{ return this->processID; }

		const std::string& getDialScript() const			{ return this->dialScript; }
		const std::string& getCtrlScript() const			{ return this->ctrlScript; }

		const std::string& getBindAddressSlave() const		{ return this->bindAddressSlave; }
		const std::string& getBindAddressDevice() const		{ return this->bindAddressDevice; }

//		const std::string& getFolderMessageStored() const	{ return this->folderMessageStored; }
//		const std::string& getFolderMessageIncoming() const	{ return this->folderMessageIncoming; }
		const std::string& getMessageGreeting() const		{ return this->messageGreeting; }

		/**
		 * @brief   Get IVGettyAMListener interface class for callbacks.
		 * @return  pointer to object which implements callback-functions
		 */
		IVGettyAMListener* getVGettyAMListener();

//@}
/** @name Answering Machine Interface
 * @{
 ******************************************************************************/
	public:
		virtual bool isEnabled() const;

		virtual WRESULT turnOn();
		virtual WRESULT turnOff();

	//	virtual WRESULT deleteMessageIncoming( const std::string& message_file );
	//	virtual WRESULT deleteMessageStored( const std::string& message_file );

		
		virtual WRESULT reloadMessageIncoming();
		virtual WRESULT reloadMessageStored();

		virtual WRESULT reportNewMessages( const std::string& phone_number );


		const tVoiceCallList& getCalls() const					{ return this->activeCalls; }
		tVoiceCallList& getCalls()								{ return this->activeCalls; }

		const tAudioMessageList& getMessageIncoming() const		{ return this->messagesIncoming; }
		tAudioMessageList& getMessageIncoming()					{ return this->messagesIncoming; }

		const tAudioMessageList& getMessageStored() const		{ return this->messagesStored; }
		tAudioMessageList& getMessageStored()					{ return this->messagesStored; }

//@}
/** @name Phone Interface - Voice Calls
 * @{
 ******************************************************************************/
	public:

		virtual WRESULT dialCallAndPlay( const std::string& number, const std::string& message_wav );

		virtual WRESULT dialCall( const std::string& phoneNumber, unsigned int call_id = 0 );
		virtual WRESULT hangUpCall( unsigned int call_id = 0 );
		virtual WRESULT answerCall( unsigned int call_id = 0 );

//@}
/** @name ISocketTcpListener
 * @{
 ******************************************************************************/
	public:
		virtual void udpIncomingDatagram( const char * data, int maxSize, const std::string& source, wosh::network::SocketUdp* socket );
//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		virtual WRESULT reloadMessagesFromFolder( const std::string& folder, tAudioMessageList& msglist );

		/**
		 * @brief   Control the process sending command passed as argument.
		 * @param command command supported by Control-Script (isenabled, enable, disable, reboot)
		 * @see     _AnsweringMachineVgetty_KEY_CtrlScript (/bin/scripts/amvgettyctrl.sh)
		 * @return  WRET_NOT_IMPLEMENTED or WRET_ERR_ILLEGAL_USE if feature is not available;
		 *          WRET_ERR_INTERNAL if something went wrong; WRET_OK if sms was sent.
		 */
		virtual WRESULT controlVgettyProcess( const std::string& command );

//@}
/** @name Modem Interface
 * @{
 ******************************************************************************/
	public:

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		/**
		 * @brief   Perform periodic checkConnected() to update stats.
		 * @see     running
		 * @see     autoRefreshFrequency
		 */
		virtual void runThread();


//@}

	protected:

		IVGettyAMListener* deviceListener;	///< callbacks for stats (such as VGettyAMBundle*)
		wosh::MutexRW configMux;			///< Mutex of configuration (previous vars)

		mutable bool vgettyEnabled;

		mutable std::string processID;
		std::string dialScript;
		std::string ctrlScript;

		std::string folderMessageIncoming;
		std::string folderMessageStored;

		std::string messageGreeting;

		std::string bindAddressSlave;
		std::string bindAddressDevice;

		tVoiceCallList activeCalls;

		tAudioMessageList messagesIncoming;
		tAudioMessageList messagesStored;

		wosh::network::SocketUdp* udpSocket;


}; // class def

#endif //__WOSH_DEVICES_VGettyAM_H__
